let userObj = sessionStorage.getItem("token") ? JSON.parse(sessionStorage.getItem("token")) : {};

if(userObj.isLogin !== "ok"){
    location.href = "enroll.html";
}else{
    // 登录
    let userObj = sessionStorage.getItem("token") ? JSON.parse(sessionStorage.getItem("token")) : {};

    if(userObj.isLogin === "ok"){
        $(".loginMsg").hide();
        $(".userMsg").show();
        $(".userMsg").children("span").html(userObj.username);
    }

    $(".userMsg").children("a").click(function(){
        sessionStorage.setItem("token","");
        $(".loginMsg").show();
        $(".userMsg").hide();
    })
}



// 鼠标进入改变字体颜色
$(".top-ul").find("li").hover(function(){
    $(this).children().css("color","#4bcdc1").next("ul").css("display","block")
},function(){
    $(this).children().css("color","#000").next("ul").css("display","none")
});





  

// $(".top-ul").children("li").hover(function(){
//     // console.log(this);
//     $(this).children("ul").css("display","block")
// })
    // ;(function(){

        // logo 端口搜索
        // console.log($(".logo-txt"))
    $(".logo-txt").on("input",function(){
        // console.log($(this).val())
        if($(this).val() === ""){
            $(".logo-right>ul").empty();
            return;
        }
        $.ajax({
            url:"https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su",
            dataType:"jsonp",
            jsonp:"cb",
            data:{
                wd:$(this).val()
            },
            success:(res)=>{
                // console.log(res.s);
                // res.s
                let str = "";
                for(let i=0;i<res.s.length;i++){
                    str += `<li>${res.s[i]}</li>`
                }
                // console.log(str)
                $(".logo-right>ul").html(str);
            }
        })
    })

    // 点击跳转
    // $(".logo-right>span").click(function(){
    //     location.href = "https://www.baidu.com/s?wd="+ $(".logo-txt").val()
    // })
    // });

    // nav 事件
    $(".nav-first").children("li").hover(function () {
        $(this).children("a").css("color","#4bcdc1").parent().css("background","#ffff").children(".navIn").css("display","block")
        
    },function () {
        $(this).children("a").css("color","#fff").parent().css("background","#4bcdc1").children(".navIn").css("display","none")
        
    })








    // 购物车
    class Cart {
        constructor() {
            this.url = "data/data.json";
            this.tbody = document.querySelector(".cartBottom");
    
            this.load();
            this.addEvent();
        }
        addEvent(){
            let that = this;
            this.tbody.addEventListener("click",function(eve){
                let e = eve || window.event;
                let tar = e.target || e.srcElement;
                if(tar.className === "remove"){
                    // 获取删除按钮所在数据的id
                    that.id = tar.parentNode.getAttribute("data-id");

                    // 删除DOM元素
                    tar.parentNode.parentNode.parentNode.parentNode.remove();
                    // 准备更新cookie中的数据
                    that.setData(0);
                }
            })
            this.tbody.addEventListener("input",function(eve){
                let e = eve || window.event;
                let tar = e.target || e.srcElement;
                if(tar.className === "changNum"){
                    // 获取数量按钮所在数据的id
                    that.id = tar.parentNode.parentNode.getAttribute("data-id");
                    // 获取最新的数量
                    that.num = tar.value;
                    // 准备更新cookie中的数据
                    that.setData(1);
    
                    // 计算总价 = 数量 * 单价
                    tar.parentNode.nextElementSibling.innerHTML = 
                    that.num * tar.parentNode.previousElementSibling.innerHTML;
                }
            })
        }
        setData(type){
            // 找到当前操作的商品信息的数据
            // console.log(1)
            let i = 0;
            this.proData.some((val,idx)=>{
                i = idx;
                return val.id === this.id;
            })
            if(type === 0){
                // 根据索引，从数组中删除对应数据
                this.proData.splice(i, 1);
            }else{
                // 根据索引，从数组中更新对应数据
                this.proData[i].num = this.num;
            }
            // 一定要将这个数组，重新设置到cookie
            setCookie("proData", JSON.stringify(this.proData));
        }
        load() {
            request({
                url: this.url,
                success: (res) => {
                    // console.log(res);
                    this.res = JSON.parse(res);
                    // 准备读取cook数据
                    this.getData();
                }
            })
        }
        getData() {
            // 判断cook中是否有数据，若有数据就解析，若无数据就给个空数组
            this.proData = getCookie("proData") ? JSON.parse(getCookie("proData")) : [];
            // console.log(this.proData);
            // console.log(this.res);
            this.render();
        }
        render() {
            // console.log(this.proData);
            // console.log(this.res);
            let str = "";
            this.proData.forEach(val1 => {
                // console.log(val1.id)
                this.res.forEach(val2 => {
                    // console.log(val2.name)
                    if (val1.id === val2.goodsId) {
                        str += `<div><div class="cartBottomF" data-id="${val2.goodsId}">
                        <input type="checkbox"><em class="">店铺：</em><a href="#">iPhon专卖店</a><i class="iconfont icon-youhuiquanwenzi"></i>
                    </div>
                    <div class="carBottomL">
                        <div class="Cl">
                            <input type="checkbox" >
                            <span><img src="${val2.img[0].small}" alt=""></span>
                        </div>
                        <ol>
                            <li>${val2.name}</li>
                            <li>型号</li>
                            <li>${val2.proce}</li>
                            <li><input type="number" min="1" value="${val1.num}" class="changNum"></li>
                            <li>￥${val2.proce * val1.num}</li>
                            <li>
                                <p>移入收藏夹</p>
                                <p class="remove">删除</p>
                                <p>相似宝贝</p>
                            </li>
                        </ol>
                    </div></div>`
                    }
                })
            });
            // console.log()
            $(".cartBottom").append(str)
        }
    }
    new Cart();    
